package game.utils;

import game.model.Card;
import game.model.Deck;
import game.player.Player;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Util {

	// GAME RELATED CONSTANTS
	public static final int MIN_NUM_USERS = 2;
	public static final int MAX_NUM_USERS = 4;
	
	// LANGUAGE RELATED CONSTANTS
	public static final String ENGLISH = "English";
	public static final String SPANISH = "Spanish";
	public static final String LANGUAGE = "Language";
	
	// GUI RELATED CONSTANTS
	public static final int MIN_WINDOW_HEIGHT = 200;
	public static final int SETTINGS_WINDOW_HEIGHT = 300;
	public static final int SETTINGS_WINDOW_WIDTH = 400;
	public static final int NEWGAME_WINDOW_HEIGHT = 400;
	public static final int NEWGAME_WINDOW_WIDTH = 450;
	
	//GUI MENU LABELS
	public static String FILE = "File";
	public static String SAVE_GAME = "Save Game";
	public static String LOAD_GAME = "Load Game";
	public static String NEW_GAME = "New Game";
	public static String SETTINGS = "Settings";
	public static String DIFFICULTY = "Difficulty";
	public static String OKAY = "Okay";
	public static String COLOR = "Color";
	public static String GREEN = "Green";
	public static String RED = "Red";
	public static String BLUE = "Blue";
	public static String YELLOW = "Yellow";
	public static String WILD = "Wild";
	public static String PURPLE = "Purple";
	public static String PLAYER_1 = "Player 1";
	public static String PLAYER_2 = "Player 2";
	public static String PLAYER_3 = "Player 3";
	public static String PLAYER_4 = "Player 4";
	public static String ONE = "1";
	public static String TWO = "2";
	public static String THREE = "3";
	public static String FOUR = "4";
	public static String REAL = "Real";
	public static String COMPUTER = "Computer";
	public static String NONE = "None";
	public static String CARDS = "Cards";
	public static String SELECT_COLOR = "Select a Color!";
	public static String GAME_OVER = "Game Over";
	
	
	
	//POSSIBLE COLOR OF A CARD
	public static final Color[] colors = {Color.BLUE, Color.GREEN, Color.RED, Color.YELLOW, Color.BLACK};
	
	
	/**
	 * Saves the game to a set location.
	 * @return boolean, true if the game was saved and false if it was unable to save.
	 * @param String[] p, contains the players
	 * @param Deck deckUp, the discard deck.
	 * @param Deck deckDown, the draw pile.
	 */
	public boolean saveGame(Player[] p, Deck deckUp, Deck deckDown) {
		
		return false;
	}
	
	/**
	 * Saves the game to a set location.
	 * @return boolean, true if the game was saved and false if it was unable to save.
	 * @param String s, The location to save the file.
	 * @param String[] p, contains the players
	 * @param Deck deckUp, the discard deck.
	 * @param Deck deckDown, the draw pile.
	 */
	public boolean saveGame(String s, Player[] p, Deck deckUp, Deck deckDown) {
		BufferedWriter writer = null;
		int cardCounter = 0;
		
		if(p.length < 2 && !isValidSaveLocation(s))
			return false;
		
		try {
			writer = new BufferedWriter(new FileWriter(s));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String players = "";
		String isHuman = "";
		for(int x = 0; x < p.length; x++) {
			if(x != p.length - 1) {
				players = players.concat(p[x].getName() + ", ");
				String status;
				if(p[x].isHuman())
					status = "true, ";
				else
					status = "false, ";
			}
			else {
				players = players.concat(p[x].getName());
				String status;
				if(p[x].isHuman())
					status = "true";
				else
					status = "false";
			}
		}
		
		try {
			writer.write(players);
			writer.newLine();
			writer.write(isHuman);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		for(int x = 0; x < p.length; x++) {
			StringBuffer cards = new StringBuffer();
			if(p[x].hasNumCards() != 0) {
				cards.append(p[x].getAllCards());
				cardCounter += p[x].hasNumCards();
			}
			else {
				cards.append("null, null");
			}
			
			try {
				writer.newLine();
				writer.write(cards.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		StringBuffer up = new StringBuffer(deckUp.getCounter());
		StringBuffer down = new StringBuffer(deckDown.getCounter());
		int upCounter = deckUp.getCounter();
		int downCounter = deckDown.getCounter();
		
		for(int x = 0; x < upCounter; x++) {
			if(deckUp.peek() != null && x != deckUp.getCounter() - 1) {
				Card temp = deckUp.pop();
				up.append(temp.getColor().toString() + " - " + temp.getValue() + ", ");
				deckUp.push(temp);
			}
			else if(deckUp.peek() != null){
				Card temp = deckUp.pop();
				up.append(temp.getColor().toString() + " - " + temp.getValue());
				deckUp.push(temp);
			}
		}
		
		for(int x = 0; x < downCounter; x++) {
			if(deckDown.peek() != null && x != deckDown.getCounter() - 1) {
				Card temp = deckDown.pop();
				up.append(temp.getColor().toString() + " - " + temp.getValue() + ", ");
				deckUp.push(temp);
			}
			else if(deckDown.peek() != null){
				Card temp = deckDown.pop();
				up.append(temp.getColor().toString() + " - " + temp.getValue());
				deckUp.push(temp);
			}
		}
		
		try {
			writer.newLine();
			writer.write(up.toString());
			writer.newLine();
			writer.write(down.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return true;
	}
	
	
	/**
	 * Saves the game to a set location.
	 * @return List<Object>, A list with all of the items for a new game. The first object is a boolean, true if the game can be loaded.
	 * @param String s, The location of the file to be loaded.
	 */
	public List<Object> loadGame(String s) {
		List<Object> stuff = new ArrayList<Object>();
		boolean validList = false;
		stuff.add(validList);
		
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(s));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		StringBuilder sb = new StringBuilder();
        String line = "";
		try {
			line = br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        String[] names = new String[Util.MAX_NUM_USERS];
        
        int lastBreak = 0;
        int commas = 0;
        for(int x = 0; x < line.length(); x++) {
        	if(line.charAt(x) == ',') {
        		names[commas] = line.substring(lastBreak, x);
        		lastBreak = x + 2;
        		commas++;
        	}
        	else if(x == line.length() - 1) {
        		names[commas] = line.substring(lastBreak, x);
        		commas++;
        	}
        }
        
        stuff.add(names);
        
        try {
			line = br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        boolean[] isHuman = new boolean[names.length];
        String[] isHumanString = new String[names.length];
        
        lastBreak = 0;
        commas = 0;
        for(int x = 0; x < line.length(); x++) {
        	if(line.charAt(x) == ',') {
        		isHumanString[commas] = line.substring(lastBreak, x);
        		lastBreak = x + 2;
        		commas++;
        	}
        	else if(x == line.length() - 1) {
        		isHumanString[commas] = line.substring(lastBreak, x);
        		commas++;
        	}
        }
        
        for(int x = 0; x < isHumanString.length; x++) {
        	if(isHumanString[x].equalsIgnoreCase("true"))
        		isHuman[x] = true;
        	else if(isHumanString[x].equalsIgnoreCase("false"))
        		isHuman[x] = false;
        }
        
        stuff.add(isHuman);
		
		
		
		return stuff;
	}
	
	private boolean isValidSaveLocation(String s) {
		return true;
	}
	
}
